<template>
  <!-- 新增模块 -->
  <el-dialog
    :close-on-press-escape="true"
    :visible.sync="insideFlage"
    @close=" addChildFormConfig.cancel()"
    class="vc-dialog-form"
    destroy-on-close
    title="新增模块"
    width="480px"
  >
    <supForm :formConfig=" addChildFormConfig" ref="addChildForm" />
    <div class="dialog-footer" slot="footer">
      <el-button size="small" @click=" addChildFormConfig.cancel()">取消</el-button>
      <el-button size="small" :disabled="disabled" @click=" addChildFormConfig.ok()" type="primary">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import supForm from '_c/form'
export default {
  name: 'addModel',
  components: { supForm },
  props: {
    addChildModelFlage: {
      type: Boolean,
      default () {
        return false
      }
    },
    selectNode: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      insideFlage: false,
      disabled: false,
      // 增加子模块
      addChildFormConfig: {
        items: [
          {
            title: '名称',
            key: 'name',
            maxlength: 20,
            required: true,
            message: '请输入'
          }
        ],
        params: {},
        cancel: () => {
          this.$refs.addChildForm.resetForm()
          this.insideFlage = false
          this.$emit('close', false)
        },
        ok: () => {
          let flage = this.$refs.addChildForm.save()
          if (flage) {
            this.disabled = true
            this.$api
              .addchildGroup({
                data: this.addChildFormConfig.params
              })
              .then(res => {
                if (res.success) {
                  this.$message.success('新增成功')
                  this.$refs.addChildForm.resetForm()
                  this.insideFlage = false
                  this.$emit('close', res)
                } else {
                  this.disabled = false
                }
              })
              .catch(err => {
                this.disabled = false
              })
          }
        }
      }
    }
  },
  computed: {},
  watch: {
    addChildModelFlage (value) {
      if (value) {
        this.init(value)
      }
    }
  },
  methods: {
    init (value) {
      this.insideFlage = value
      this.disabled = false
      if (this.selectNode.isChild) {
        this.addChildFormConfig.params.pid = this.selectNode.id
      } else {
        this.addChildFormConfig.params.pid = this.selectNode.pid
      }
    }
  }
}
</script>
